import * as Cesium from 'cesium';
export class CircleWaveMaterialProperty {
  constructor(options) {
    this.definitionChanged = new Cesium.Event();
    Cesium.Material._materialCache.addMaterial('CircleWaveMaterial', {
      fabric: {
        type: 'CircleWaveMaterial',
        uniforms: {
          color: options.color || new Cesium.Color(1, 0, 0, 1), // 红色告警
          count: 3, // 多圈叠加
          gradient: 1.5, // 渐变更柔和
        },
        source: `
          czm_material czm_getMaterial(czm_materialInput materialInput) {
            czm_material material = czm_getDefaultMaterial(materialInput);
            vec2 st = materialInput.st;
            float dis = distance(st, vec2(0.5, 0.5));
            float time = fract(czm_frameNumber / 120.0); // 动画更快
            float per = time;
            float alpha = 0.0;
            float rings = float(count);
            for (int i = 0; i < 6; i++) {
              if (float(i) < rings) {
                float cur = fract(per + float(i) / rings);
                float radius = cur * 0.5;
                float diff = dis - radius;
                float edge = 0.03 + 0.01 * cur; // 波纹宽度随扩散略变
                float ringAlpha = smoothstep(edge, 0.0, abs(diff));
                ringAlpha *= pow(1.0 - cur, gradient); // 越靠近中心越亮
                alpha += ringAlpha;
              }
            }
            alpha = clamp(alpha, 0.0, 1.0);
            material.diffuse = color.rgb;
            material.alpha = alpha * color.a;
            return material;
          }
        `,
      }
    });
  }
  getType () {
    // 返回材质类型
    return 'CircleWaveMaterial';
  }
  getValue (time, result) {
    // result.uTime = this.params.uTime;
    // 返回材质值
    return result;
  }
  equals (other) {
    // 判断两个材质是否相等
    return (
      other instanceof CircleWaveMaterialProperty && this.name === other.name
    );
  }
}
